package com.home.cop.base;

import com.home.cop.sysinfo.model.UserSession;
import com.home.cop.system.BusinessException;
import com.home.cop.system.Result;
import com.home.cop.utils.StringEscapeEditor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

public class BaseController {
        protected HttpServletRequest request;

        protected UserSession userSession;

        protected HttpServletResponse response;

        private int pageSize = 10;
        private int pageNum = 1;
        @InitBinder
        public void initBinder(ServletRequestDataBinder binder) {
            /**
             * 自动转换日期类型的字段格式
             */
            binder.registerCustomEditor(Date.class,
                    new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));

            /**
             * 防止XSS攻击
             */
            binder.registerCustomEditor(String.class, new StringEscapeEditor(true, false));
        }

        public int getPageSize() {
            request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
            if (StringUtils.isNotBlank(request.getParameter("pageSize"))){
                pageSize = Integer.valueOf(request.getParameter("pageSize"));
            }
            return pageSize;
        }

        public void setPageSize(int pageSize) {
            this.pageSize = pageSize;
        }

        public int getPageNum() {
            request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
            if (StringUtils.isNotBlank(request.getParameter("pageNum"))){
                pageNum = Integer.valueOf(request.getParameter("pageNum"));
            }
            return pageNum;
        }

        public void setPageNum(int pageNum) {
            this.pageNum = pageNum;
        }
        public HttpServletRequest getRequest() {
            request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
            return request;
        }

        public UserSession getUserSession() {
            HttpSession session = getRequest().getSession();
            this.userSession = (UserSession) session.getAttribute("userSession");
            return userSession;
        }

        public void setUserSession(UserSession userSession) {
            this.userSession = userSession;
        }

        public UserSession RoleCheck(){
            UserSession userSession = getUserSession();
            if (userSession.getInstId() != 0 ){
                throw new BusinessException("无权限！");
            }
            return userSession;
        }
}
